**********************************************************************
**********************************************************************
*********************** THIS IS DO FILE 9 ****************************
****************** THIS FILE CREATES THE INDICES *********************
**********************************************************************
**********************************************************************


*Version: 2023 March 29

cap mkdir `"$output/ffs_mco_separation/temp"'
cap mkdir `"$output/ffs_mco_separation/log/9_indices"'
cap mkdir `"$output/ffs_mco_separation/indices"'

	
cap log close 
log using `"$output/ffs_mco_separation/log/9_indices/9_indices.smcl"', replace 

di "This run uses code version from 2023/03/29"


**********************************************************************
**********************************************************************
*************************** CIP INDICES ******************************
**********************************************************************
**********************************************************************

use `"$output/ffs_mco_separation/data/regression_data.dta"', clear 

destring COUNTY, gen(FIPS)

global CIP_RHS_index = `"i(1/2).pay_type_pooled#ibn.state i(1/2).pay_type_pooled#c.RVU i(1/2).pay_type_pooled#ibn.size_indicator patient_age charlson i.year"'
global RHS_index_probit = `"patient_age charlson i.year"'


// generate empty variables to save estimates later
local version `" "" "_rf" "' // version with and without model estimates 

foreach v of local version { 
	
	gen lev_CIP_FE`v' = .
	gen lev_CIP_FE_SE`v' = .
	
	gen lev_CIP_FE_nofe`v' = .
	gen lev_CIP_FE_SE_nofe`v' = .
	
}


gen v_initial_den_CIP = .
gen v_initial_den_CIP_SE = .



lab var lev_CIP_FE "State CIP Index w/ Phys FE size contr."

lab var pay_type_pooled "1: Medicare, 2: Medicaid, 3: Commercial"



*****************************************
***** Generate Heckmann First Stage *****
*****************************************
//merge variables for first stage  
merge m:1 year FIPS using `"$control_var/county_controls.dta"'
keep if _merge == 3 // all data from master file merge 
drop _merge 

//create variables
gen share_medicaid_elig = mean_number_mediciad_elig/county_pop
gen medicaid_visit = 0 
replace medicaid_visit = 1 if pay_type_pooled == 2 

//estimate probit 
probit medicaid_visit share_medicaid_elig $RHS_index_probit
	
predict probitxb, xb
gen pdf = normalden(probitxb)
gen cdf = normal(probitxb)
gen imr = pdf/cdf


**************************************
**************************************
****** FUNCTIONAL FORM \tau **********
**************************************
**************************************


********************
**** Unweighted ****
********************
	
foreach v of local version { 
	
	sum tau`v', d 
	local com_mean `r(mean)' //the indices will be normalized 
	
	// CPU index regression w/ phys FE
	_regress tau`v' $CIP_RHS_index, absorb(idw_provider_id)

	forvalues x = 1/2 {
				
		forvalues y = 1/51 {
				
			cap replace lev_CIP_FE`v'       =   _b[`x'.pay_type_pooled#`y'.state] + `com_mean' if pay_type_pooled == `x' & state == `y'
			cap replace lev_CIP_FE_SE`v'    =   _se[`x'.pay_type_pooled#`y'.state] if pay_type_pooled == `x' & state == `y'
			
		}
	}
	
	
	// CPU index regression w/o FE but with Heckmann correction 
	reg tau`v' $CIP_RHS_index imr

	forvalues x = 1/2 {
				
		forvalues y = 1/51 {
				
			cap replace lev_CIP_FE_nofe`v'       =   _b[`x'.pay_type_pooled#`y'.state] + `com_mean' if pay_type_pooled == `x' & state == `y'
			cap replace lev_CIP_FE_SE_nofe`v'    =   _se[`x'.pay_type_pooled#`y'.state] if pay_type_pooled == `x' & state == `y'
			
		}
	}

}



*****************************
**** Denial Rate Indices ****
*****************************
// Denial Rate Index -- Initial Denial Rate (Physician FE)
_regress visit_initial_den $CIP_RHS_index, absorb(idw_provider_id)

forvalues x = 1/2 {
			
	forvalues y = 1/51 {
			
		cap replace v_initial_den_CIP      =   _b[`x'.pay_type_pooled#`y'.state] + _b[_cons] if pay_type_pooled == `x' & state == `y'
		cap replace v_initial_den_CIP_SE    =   _se[`x'.pay_type_pooled#`y'.state] if pay_type_pooled == `x' & state == `y'
		
	}
}


//generate raw means 
bys pay_type_pooled payee_state_cd: gegen mean_tau = mean(tau)
bys pay_type_pooled payee_state_cd: gegen mean_CIP = mean(CIP)
bys pay_type_pooled payee_state_cd: gegen mean_denial_rate = mean(visit_initial_den)

bys payee_state_cd pay_type_pooled: keep if _n == 1 
keep  *CIP_FE* payee_state_cd state pay_type_pooled v_*_den_CIP* mean_??? mean_denial_rate

save `"$output/ffs_mco_separation/indices/CIP_indices.dta"', replace 


keep if pay_type_pooled == 2 // keep medicaid
bys payee_state_cd: keep if _n == 1 
save `"$output/ffs_mco_separation/indices/CIP_indices_medicaid.dta"', replace 



**********************************************************************
**********************************************************************
*************************** FEE INDICES ******************************
**********************************************************************
**********************************************************************


use `"$output/data/$line_file"', clear 

********************************************************
****** Fee Index at Line Level from Main Dataset *******
********************************************************
//gen final variables
gen pi_c_FE_cpt_line = .
gen pi_c_FE_cpt_line_SE = .

lab var pi_c_FE_cpt_line "Unweighted Fee Index without imputations"

//gen variables/specify controls for index computation
global RHS_fee_index = `"i(1/2).pay_type_pooled#ibn.state patient_age charlson i.year"'
gen ln_line_item_value = log(line_item_value)


//commercial fee mean unweighted
tabstat ln_line_item_value if imputed_value == 0, stat(mean) save
matrix stats = r(StatTotal)
local com_mean_pi_c = stats[1,1]
di `com_mean_pi_c'


**************************************
*********** Fee w/ CPT FE ************
**************************************
// Fee index regression w/ CPT FE only no imputed values
_regress ln_line_item_value $RHS_fee_index if imputed_value == 0 , absorb(proc1)


forvalues x = 1/2 {
			
	forvalues y = 1/51 {
			
		cap replace pi_c_FE_cpt_line       =   _b[`x'.pay_type_pooled#`y'.state] + `com_mean_pi_c' if pay_type_pooled == `x' & state == `y'
		cap replace pi_c_FE_cpt_line_SE    =   _se[`x'.pay_type_pooled#`y'.state] if pay_type_pooled == `x' & state == `y'
		
	}
}


bys payee_state_cd pay_type_pooled: keep if _n == 1 

bys payee_state_cd pay_type_pooled: keep if _n == 1 
save `"$output/ffs_mco_separation/indices/fee_indices.dta"', replace 

keep if pay_type_pooled == 2 
save `"$output/ffs_mco_separation/indices/fee_indices_medicaid.dta"', replace



log close 
